const db = require('../dbbase/db');
const getData = require("../util/data");
const moment = require('moment');


//获取异常信息
exports.getUnusualInfo = (req, res, next) => {
   let startDate = '';
   let endDate = '';
   if (req.body.category === 'mouth') {
      startDate = getData.getCurrentMonthFirst();
      endDate = getData.getCurrentMonthLast();
   } else if (req.body.category === 'day') {
      startDate = getData.getCurrentDayStart();
      endDate = getData.getCurrentDayEnd();
   }

   const sql = "select m.mtname,m.mtcode,s.startname,t.TOUCHTIME,t.ENDTIME,t.STATUS from LP_SCENC_INFO t "
      + "join mt_baseinfo m on t.mtid = m.id "
      + "join LP_SCENC_CONFIG s on s.id = t.scencsetid "
      + "where (endtime >= to_date('" + startDate + "', 'yyyy-mm-dd hh24:mi:ss') or endtime is null) and touchtime < to_date('" + endDate + "', 'yyyy-mm-dd hh24:mi:ss') "
      + "and m.deptid='9' "
      + "and MTTYPE='JJSB' "
      + "ORDER BY TOUCHTIME DESC "
   const params = [];
   db.query(sql, params).then((result) => {
      let totleTime = '';
      for (let i = 0; i < result.length; i++) {
         if (result[i].ENDTIME === null) {
            result[i].ENDTIME = moment().format('YYYY-MM-DD HH:mm:ss');
         } else {
            result[i].ENDTIME = moment(result[i].ENDTIME).format('YYYY-MM-DD HH:mm:ss');
         }
         result[i].TOUCHTIME = moment(result[i].TOUCHTIME).format('YYYY-MM-DD HH:mm:ss');
         let time = new Date(result[i].ENDTIME) - new Date(result[i].TOUCHTIME);
         result[i].totleTime = parseFloat((time / 1000 / 60).toFixed(2));
      }
      
      const deviceList = [];
      const planList = [];
      const changeList = [];
      const qulityList = [];
      const changeKnifeList = [];
      const matterList = [];
      for (let i = 0; i < result.length; i++) {
         if (result[i].STARTNAME === "设备故障") {
            deviceList.push(result[i])
         }
         if (result[i].STARTNAME === "计划停机") {
            planList.push(result[i])
         }
         if (result[i].STARTNAME === "更换工装") {
            changeList.push(result[i])
         }
         if (result[i].STARTNAME === "质量调整") {
            qulityList.push(result[i])
         }
         if (result[i].STARTNAME === "更换刀具") {
            changeKnifeList.push(result[i])
         }
         if (result[i].STARTNAME === "物料等待") {
            matterList.push(result[i])
         }
      }
      var deviceListTotleTime = 0;
      let planListTotleTime = 0;
      let changeListTime = 0;
      let qulityListTime = 0;
      let changeKnifeListTime = 0;
      let matterListTime = 0;
      for (let i=0;i < deviceList.length; i++) {
         deviceListTotleTime = deviceListTotleTime + deviceList[i].totleTime
      }
      console.log(deviceListTotleTime);
      for (i in planList) {
         planListTotleTime += planList[i].totleTime
      }
      for (i in changeList) {
         changeListTime += changeList[i].totleTime
      }
      for (i in qulityList) {
         qulityListTime += qulityList[i].totleTime
      }
      for (i in changeKnifeList) {
         changeKnifeListTime += changeKnifeList[i].totleTime
      }
      for (i in matterList) {
         matterListTime += matterList[i].totleTime
      }
      const productList = [
         { index: '0', category: '设备故障', list: deviceList, sum: deviceList.length, totleTime: deviceListTotleTime, a: '1' },
         { index: '1', category: '计划停机', list: planList, sum: planList.length, totleTime: planListTotleTime, a: '1' },
         { index: '2', category: '更换工装', list: changeList, sum: changeList.length, totleTime: changeListTime, a: '1' },
         { index: '3', category: '质量调整', list: qulityList, sum: qulityList.length, totleTime: qulityListTime, a: '1' },
         { index: '4', category: '更换刀具', list: changeKnifeList, sum: changeKnifeList.length, totleTime: changeKnifeListTime, a: '1' },
         { index: '5', category: '物料等待', list: matterList, sum: matterList.length, totleTime: matterListTime, a: '1' }
      ];
      res.status(200).json(productList)
   }).catch((err) => {
      console.log(err);
   });
}
//查询该精车某种异常类型信息列表
exports.getUnusualInfoList = (req, res, next) => {
   const startDate = getData.getCurrentMonthFirst();
   const endDate = getData.getCurrentMonthLast();
   const name = req.body.name;
   const category = req.body.category;
   const sql = "select m.mtname,m.mtcode, s.startname,s1.startname as category,t.TOUCHTIME,t.RESPONSETIME,t.TRIALTIME,t.ENDTIME,ceil(((t.ENDTIME - t.TOUCHTIME)) * 24 * 60) as totleTime,t.STATUS,u.F_REALNAME from LP_SCENC_INFO t "
      + "left join LP_SCENC_LOG l on t.id = l.SCENCID "
      + "join mt_baseinfo m on m.id = t.MTID "
      + "join LP_SCENC_CONFIG s on s.id = t.scencsetid "
      + "left join LP_SCENC_CONFIG  s1 on s1.id = l.scenctype "
      + "left join 	LP_SCENC_WORKINFO w on w.SCENCID = t.id "
      + "left join LR_BASE_USER u on u.F_USERID = w.WORKID "
      + "where (endtime >= to_date('" + startDate + "', 'yyyy-mm-dd hh24:mi:ss') or endtime is null) and touchtime < to_date('" + endDate + "', 'yyyy-mm-dd hh24:mi:ss') "
      + "and m.deptid='9' "
      + "and m.mtname='" + name + "'"
      + "and s.STARTNAME='" + category + "'"
      + "and MTTYPE='JJSB' "
      + "ORDER BY TOUCHTIME DESC "
   const params = [];
   db.query(sql, params).then((result) => {
      for (let i = 0; i < result.length; i++) {
         result[i].TOUCHTIME = moment(result[i].TOUCHTIME).format('YYYY-MM-DD HH:mm:ss');
         result[i].ENDTIME = moment(result[i].ENDTIME).format('YYYY-MM-DD HH:mm:ss');
         result[i].RESPONSETIME = moment(result[i].ENDTIME).format('YYYY-MM-DD HH:mm:ss');
         result[i].TRIALTIME = moment(result[i].ENDTIME).format('YYYY-MM-DD HH:mm:ss');
      }
      res.status(200).json(result)
   }).catch((err) => {
      console.log(err);
   });

}